<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-26 16:41:21
 * @version 1.0
 */

class HomeAction extends BaseAction {
    
    protected $_user;      //用户信息
    protected $is_weixin;  //是否为微信浏览器
  
    public function _initialize(){

        parent::_initialize();

        //正式使用时注释掉
        //import("ORG.Crypt.Xxtea");
        //cookie('memberID',Xxtea::encrypt('104312',C('COOKIE_PREFIX')));

        /*$this->_user = $this->getUserStatus(); //用户的状态
        $this->assign('_user',$this->_user);

        //判断是否为微信浏览器
        $agent = $_SERVER['HTTP_USER_AGENT']; 
        if(strpos($agent,"icroMessenger")) {
            $this->is_weixin = true;
        }else{
            $this->is_weixin = false;
        }

        //是否强制在微信中访问
        if ($this->is_weixin==false) {
            redirect(U('Subscribe/index'));
        }

        //先拿到用户的openid
        if (!$this->_user['status']) {
            $goToUrl = C('site_url').__SELF__;                
            $code = I('get.code');
            if ($code=='') {
                $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.C('APP_ID').'&redirect_uri='.$goToUrl.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';  
                echo "<script language='javascript' type='text/javascript'>";  
                echo "window.location.href='$url'";  
                echo "</script>";
            }else{
                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.C('APP_ID').'&secret='.C('APP_SECRET').'&code='.$code.'&grant_type=authorization_code';
                $con = file_get_contents($url);
                $con = json_decode($con);
                $openid = $con->openid; 
                $access_token = $this->get_access_token();

                $url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
                $con = file_get_contents($url);
                $con = json_decode($con);

                $subscribe = (int)$con->subscribe;
                $subscribe_time = (int)$con->subscribe_time;
                $wxid = (string)$con->openid;
                $wxface = (string)$con->headimgurl;
                $wxname = (string)$con->nickname;

                //更新用户信息
                $map['wxid'] = $wxid;
                $obj = M('Member');
                $list = $obj->where($map)->find();
                if ($list) {
                    //更新用户
                    $data['wxface']=$wxface;
                    $data['wxname']=$wxname;
                    $data['guanzhu']=$subscribe;                        
                    $memberID = $list['id'];
                    $obj->where($map)->save($data);
                }else{
                    //添加用户
                    $data['wxface']=$wxface;
                    $data['wxname']=$wxname;
                    $data['wxid']=$wxid;
                    $data['guanzhu']=$subscribe;
                    $data['createTime'] = $subscribe_time;
                    if ($data['wxid']!='') {
                        $memberID = $obj->add($data);
                    }
                }

                //设置用户cookie
                import("ORG.Crypt.Xxtea");
                cookie('memberID',Xxtea::encrypt($memberID,C('COOKIE_PREFIX')));

                $list = M('Member')->where('id='.$memberID)->find();  

                $this->_user=array(
                    'status' => true,
                    'memberID' => $list['id'],
                    'wxname' => $list['wxname'],
                    'wxid' => $list['wxid'],
                    'mobile' => $list['mobile'],
                    'subscribe' => $list['guanzhu'],
                    'wxface' => $list['wxface'],
                    'point' => $list['point'],
                );
            }
        }*/
    } 

    public function getUserStatus(){
        if (cookie('memberID')=='') {
            return $userInfoArr = array(
                    'status' => false
                );
        }else{
            import("ORG.Crypt.Xxtea");
            $memberID = Xxtea::decrypt(cookie('memberID'),C('COOKIE_PREFIX'));
            $list = M('Member')->where('id='.$memberID)->find();            
            if ($list) {                
                $userInfoArr = array(
                    'status' => true,
                    'memberID' => $list['id'],
                    'wxname' => $list['wxname'],
                    'wxid' => $list['wxid'],
                    'mobile' => $list['mobile'],
                    'subscribe' => $list['guanzhu'],
                    'wxface' => $list['wxface'],
                    'point' => $list['point'],
                    'shareTime' => $list['shareTime'],
                    'shareNum' => $list['shareNum'],
                );
            }else{
                $userInfoArr = array(
                    'status' => false
                );
            }
            return $userInfoArr;
        }        
    }
}